/*
 * SiSsingleton.cpp
 *
 *  Created on: May 4, 2013
 *      Author: dsweeney
 */


#include "SiSsingleton.h"
#include <pthread.h>
#include <semaphore.h>
#include "../CommonLib/CommonTypesLib.h"

SiSsingleton::SiSsingleton():
numNodes(0)
{
	this->mutexes = new pthread_mutex_t [NUM_SLOTS];
	this->slotStates = new CommonLibTypes::slot_state_t[NUM_SLOTS];

	this->initMutexes();
	this->initSlotStates();
}

SiSsingleton::~SiSsingleton()
{
	delete[] this->mutexes;
	delete[] this->slotStates;

	//for(int i = 0; i < this->frameStartEvent.size(); i++)
		//delete this->frameStartEvent[i];
}

void SiSsingleton::initMutexes()
{
	for(int i = 0; i < NUM_SLOTS; ++i)
		pthread_mutex_init(&(this->mutexes[i]), NULL);
}

void SiSsingleton::destroyMutexes()
{
	for(int i = 0; i<this->numNodes; ++i)
		pthread_mutex_destroy(this->frameStartEvent[i]);

	for(int i = 0; i < NUM_SLOTS; ++i)
		pthread_mutex_destroy(&(this->mutexes[i]));
}

void SiSsingleton::initSlotStates()
{
	for(int i = 0; i < NUM_SLOTS; ++i)
		this->slotStates[i] = CommonLibTypes::CLEAR;
}

pthread_mutex_t* SiSsingleton::addNode(int id)
{
	this->numNodes++;
	pthread_mutex_t* newFrameStartSignal = new  pthread_mutex_t;
	pthread_mutex_init(newFrameStartSignal, NULL);
	this->frameStartEvent.push_back(newFrameStartSignal);

	return newFrameStartSignal;
}


